package com.brave.file.controller;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;

/**
 * 2019年10月12日 12:02:51
 * 二维码生成
 */
@Component
public class QRCodeGen
{
    private static final int QRCOLOR = 0xFF000000; // 默认是黑色
    private static final int BGWHITE = 0xFFFFFFFF; // 背景颜色

    private static final int WIDTH = 800; // 二维码宽
    private static final int HEIGHT = 800; // 二维码高

    // 用于设置QR二维码参数
    private static Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>()
    {
        private static final long serialVersionUID = 1L;
        {
            put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);// 设置QR二维码的纠错级别（H为最高级别）具体级别信息
            put(EncodeHintType.CHARACTER_SET, "utf-8");// 设置编码方式
            put(EncodeHintType.MARGIN, 0);
        }
    };
    
    
    public BufferedImage drawLogoQRCode(String qrUrl)
    {
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
        try {
            MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
            // 参数顺序分别为：编码内容，编码类型，生成图片宽度，生成图片高度，设置参数
            BitMatrix bm = multiFormatWriter.encode(qrUrl, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);

            // 开始利用二维码数据创建Bitmap图片，分别设为黑（0xFFFFFFFF）白（0xFF000000）两色
            for (int x = 0; x < WIDTH; x++) {
                for (int y = 0; y < HEIGHT; y++) {
                    image.setRGB(x, y, bm.get(x, y) ? QRCOLOR : BGWHITE);
                }
            }
            // 新的图片，把带logo的二维码下面加上文字
            BufferedImage outImage = null;
            Graphics2D outg = null;
            outImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_4BYTE_ABGR);
            outg = outImage.createGraphics();
            // 画二维码到新的面板
            outg.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
            outg.dispose();
            outImage.flush();
            image = outImage;
            image.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return image;
    }


//    public static void main(String[] args) {
//        try
//        {
//            BufferedImage image = drawLogoQRCode("https://www.baidu.com");
//            图片路径
//            File qrCodePath = new File("D:" + File.separator + "baidu.png");
//            if(!qrCodePath.exists())
//            {
//                qrCodePath.mkdirs();
//            }
//            ImageIO.write(image, "png", qrCodePath);
//        }catch (IOException ioe)
//        {
//            ioe.printStackTrace();
//        }
//    }
    
    // 获取logo
//    private static InputStream getInputStream(String destUrl) {
//        InputStream inputStream = null;
//        URLConnection urlConnection = null;
//        URL url = null;
//        try {
//            url = new URL(destUrl);
//            urlConnection = url.openConnection();
//            inputStream = urlConnection.getInputStream();
//        } catch (IOException e) {
//        }
//        return inputStream;
//    }
}